package org.itstec.common.security;

import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RSAUtil {

	private static Logger logger = LoggerFactory.getLogger(RSAUtil.class);
	
	private static String publicKeyString = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtNsoREuPCQID9gix5j1EnR/4q57yHxzE6kGNfCrnPPvd/8+gjFp38P9LtMBJxWxghrQ0ivfWmQ2bVKlGKSD/iKDnEnCKCslFH9pEY8hRIOKcn1v8QTGdW8g+YB30XRSg6ldQBMs11TCrsVwIigFsSKXquYAE11dkoftGyv05JVZXPgItZ02J+b+icCaWwjkJjDZg+abVfqIGEpWt4/fe8E6whwYCBQYZYbRBLLT0UZ8AOhKaGSBb1yYjbZ30S0bYDRN7qqFNv7xJ27s+daNi0lXJ6qgL824wQLtE74gDBQZHlfQ6eJVDAbwZQIo1iF5DMhlEiHV9nOFthONr97UjNwIDAQAB";
	private static String privateKeyString = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQC02yhES48JAgP2CLHmPUSdH/irnvIfHMTqQY18Kuc8+93/z6CMWnfw/0u0wEnFbGCGtDSK99aZDZtUqUYpIP+IoOcScIoKyUUf2kRjyFEg4pyfW/xBMZ1byD5gHfRdFKDqV1AEyzXVMKuxXAiKAWxIpeq5gATXV2Sh+0bK/TklVlc+Ai1nTYn5v6JwJpbCOQmMNmD5ptV+ogYSla3j997wTrCHBgIFBhlhtEEstPRRnwA6EpoZIFvXJiNtnfRLRtgNE3uqoU2/vEnbuz51o2LSVcnqqAvzbjBAu0TviAMFBkeV9Dp4lUMBvBlAijWIXkMyG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UuuOuOxaeQbVcqtSnRtOY7WYc0guBEM2gMzoM8wK8R9wNVZ9SFMw2zvPSExlgwZMkDRnrliLZlTJ+Qn94YY5gsr6TGscyfr/s5APv9y5nY8U6ceSyYU0PATWdy4nwkY+R+h7KIU6MwIdEw6ehqox6EPFquBcLgto5rHrQqZODqQ==";

	private static PublicKey getPublicKey(String base64PublicKey) throws Exception {
        byte[] keyBytes = Base64.getDecoder().decode(base64PublicKey);
        X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePublic(spec);
    }

	private static PrivateKey getPrivateKey(String base64PrivateKey) throws Exception {
        byte[] keyBytes = Base64.getDecoder().decode(base64PrivateKey);
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePrivate(spec);
    }

    public static String encrypt(String text) {
        Cipher cipher;
        byte[] cipherText = null;
		try {
			cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
			cipher.init(Cipher.ENCRYPT_MODE, getPublicKey(publicKeyString));
			cipherText = cipher.doFinal(text.getBytes());
		} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
			logger.error("无法模式匹配"+e.getMessage());
		} catch (IllegalBlockSizeException | BadPaddingException e) {
			logger.error("doFinal异常"+e.getMessage());
		}catch (Exception e) {
			logger.error("init异常"+e.getMessage());
		}
        return Base64.getEncoder().encodeToString(cipherText);
    }

    public static String decrypt(String text) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, getPrivateKey(privateKeyString));
        byte[] cipherText = Base64.getDecoder().decode(text);
        String s = Base64.getEncoder().encodeToString(cipherText);
        return s;
    }
    
}
